<template>
	<view class="work-item bcl-white relative" :class="{ selected: item.status == 1 }">
		<!-- 用户信息 -->
		<view class="box" @click="toChatPage(item.uid)">
			<view class="uni-avatar mr-10">
				<image :src="item.avatar || defaultImage" />
			</view>

			<view>
				<view class="name bold fz-15">
					<text class="mr-5">{{ item.nickname }}</text>
					<text>| {{ item.delivery_sort }}</text>
				</view>
				<view class="date cl-second fz-12 mt-5">{{ item.created_at }}</view>
			</view>

			<!-- 好评率 -->
			<view class="percent fz-12">
				<text class="cl-second">好评率：</text>
				<text class="cl-main">{{ item.percent || 0 }}%</text>
			</view>
		</view>

		<!-- 内容 -->
		<view class="mt-10 pl-40 cl-basic fz-12" style="overflow: auto;" v-html="item.desc"></view>

		<!-- 附件 -->
		<view class="box box-between box-column-center pl-40 mt-10">
			<view @tap="toFilesPage" v-if="item.children_attachment.length">
				<text class="icon icon-annex fz-14"></text>
				<text>附件({{ item.children_attachment.length }})</text>
			</view>

			<view v-else></view>

			<view>
				<button class="uni-button uni-button-sm" @tap.stop="show = true" v-if="currentProgress < 2 && role == 'employer'">
					选TA
				</button>
			</view>
		</view>

		<!-- 中标确认模态框 -->
		<uni-dialog v-model="show" @submit="bid">
			<view class="container text-center" slot="content">
				<text>确定选择</text>
				<text class="cl-main">{{ '"' + item.nickname + '"' }}</text>
				<text>设置为中标并托管资金吗?</text>
			</view>
		</uni-dialog>
	</view>
</template>

<script>
	import uniDialog from "@/components/dialog";
	import {
		bid
	} from "@/apis/task";

	export default {
		name: "workItem",

		props: {
			item: {
				type: Object,
				default: {}
			},

			currentProgress: {
				type: Number,
				default: -1
			},

			role: {
				type: String,
				default: ""
			},

			isBounty: {
				type: Boolean,
				default: false
			}
		},

		data() {
			return {
				defaultImage: this.$config.default,
				show: false
			};
		},

		methods: {
			/**
			 * 中标
			 */
			async bid() {
				let params = {
					id: this.item.id
				};

				let {
					code,
					data
				} = await bid(params);

				if (code == 200) {
					this.$toast("操作成功~", "success");
					this.toPayPage();
					this.show = false;
				}
			},

			toPayPage() {
				uni.navigateTo({
					url: `/pay/pay?type=2&taskId=${this.item.task_id}&worker=${this.item.nickname}&workerPrice=${this.item.price}`
				});
			},

			toFilesPage() {
				let files = [];
				console.log(this.item.children_attachment);
				let children_attachment = this.item.children_attachment.map(item => {
					if(item.attachment){
						// 投标记录附件列表
						return item.attachment
					}else{
						//交付稿件
						return item
					}
				}) || [];
				console.log(children_attachment);
				uni.setStorageSync("currentFiles", children_attachment);
				uni.navigateTo({
					url: `/pages/index/files`
				});
			}
		},

		components: {
			uniDialog
		}
	};
</script>

<style lang="scss" scoped>
	.work-item {

		// 中标
		&.selected {
			background: url("https://zhihua.gun7lai.com/themes/zbj/assets/images/selecte.png") no-repeat 70% center / 120upx;
		}

		// 好评率
		.percent {
			position: absolute;
			top: 0;
			right: 0;
		}
	}
</style>
